enum keyType {
    CoinPairs = "CoinPairs",
    Address = "Address",
    CleverMoney = "CleverMoney"
}
enum ResType {
    SUCCESS,
    NOTNULL,
    INIT
}

interface CallActive {
    (type: ResType): void;
}

class BookMark {
    constructor(private readonly type: keyType) {}
    isData(): boolean {
        return !!localStorage.getItem(this.type);
    }
    setData(item: any): ResType {
        if (this.isData()) {
            const data = this.getData();
            if (data.indexOf(item) == -1) {
                data.push(item);
                localStorage.setItem(this.type, JSON.stringify(data));
                return ResType.SUCCESS;
            }
            let bol = this.delDate(item);
            // console.log('yzt_删除成功', bol);
            return ResType.NOTNULL;
        }
        localStorage.setItem(this.type, JSON.stringify([item]));
        return ResType.INIT;
    }
    getData(): any[] {
        if (this.isData()) {
            return JSON.parse(localStorage.getItem(this.type)!);
        }
        return [];
    }
    delDate(item: any): boolean {
        const itemIdx = this.getData().indexOf(item);
        const ArrList = this.getData();
        if (itemIdx > -1) {
            ArrList.splice(itemIdx, 1);
            localStorage.setItem(this.type, JSON.stringify(ArrList));
            return true;
        }
        return false;
    }
}

function collect(type: keyType, item: any, callActive: CallActive) {
    // console.log('yzt__obj', item);
    switch (type) {
        case keyType.CoinPairs:
            callActive(new BookMark(type).setData(item.id));
            break;
        case keyType.Address:
            callActive(new BookMark(type).setData(item));
            break;
        case keyType.CleverMoney:
            callActive(new BookMark(type).setData(item));
            break;
    }
}

function isItem(type: keyType, item_: any): boolean {
    const arr = new BookMark(type).getData();
    let suc = arr.some(item => item === item_);
    return suc;
}

export { collect, isItem, BookMark, keyType, ResType };
